Vue v-on Directive

একটি ইভেন্ট শ্রোতা সংযুক্ত করতে একটি উপাদানের উপর v-অন রাখুন

Example

আপনার নিজস্ব Vue সার্ভার পান

একটি বোতামে ক্লিক করার সময় একটি <div> উপাদানের পটভূমির রঙ পরিবর্তন করতে v-on নির্দেশিকা ব্যবহার করে।

<template>
  <h1>v-on Example</h1>
  <div v-bind:class="{ yelClass: bgColor }">
    <p>Click the button to change background color of this DIV box.</p>
    <button v-on:click="bgColor = !bgColor">Click</button>
    <p>bgColor property: "{{ bgColor }}"</p>
  </div>
</template>

নীচে আরো উদাহরণ দেখুন.

Definition and Usage

ইভেন্ট শ্রোতাকে সংযুক্ত করার জন্য ভি-অন নির্দেশিকাটি একটি উপাদানের উপর স্থাপন করা হয়।

একটি ইভেন্ট শ্রোতাকে v-on-এর সাথে যুক্ত করতে, আপনাকে অবশ্যই ইভেন্টের ধরণ, যেকোন পরিবর্তনকারী, এবং একটি পদ্ধতি বা অভিব্যক্তি প্রদান করতে হবে যাতে ঘটনাটি ঘটে।

যদি ভি-অন একটি সাধারণ এইচটিএমএল ট্যাগে স্থাপন করা হয়, আমরা যে ইভেন্টের ধরনগুলি শোনার জন্য সংজ্ঞায়িত করতে পারি তা হল ইনপুট, ক্লিক বা মাউসওভারের মতো নিয়মিত ইভেন্ট।

যদি v-on একটি কাস্টম কম্পোনেন্টে স্থাপন করা হয়, তাহলে আমরা যে ইভেন্টের ধরনগুলি শোনার জন্য সংজ্ঞায়িত করতে পারি তা হল সেই উপাদান থেকে নির্গত কাস্টম ইভেন্ট।

ভি-অনের সংক্ষিপ্ত রূপ: @।

Modifiers

কনভার্টার বিস্তারিত
.capture একটি বুদ্বুদ ইভেন্ট প্রথমে ক্যাপচার করা হয় যেখানে .capture মডিফায়ার সেট করা হয়।
.once পৃষ্ঠাটি লোড হওয়ার পরে ইভেন্টটি শুধুমাত্র একবার চালু হয়।
.passive DOM এলিমেন্টে প্যাসিভ: সত্য সেট করে ইভেন্ট হ্যান্ডলারকে প্যাসিভ হিসেবে চিহ্নিত করে। এর মানে হল event.preventDefault() কল করা হয়েছে কিনা তা দেখার জন্য ব্রাউজারকে অপেক্ষা করতে হবে না এবং স্ক্রোল করার মতো ঘন ঘন ইভেন্টের জন্য, ইভেন্ট হ্যান্ডলারকে প্যাসিভ-এ সেট করা কর্মক্ষমতা উন্নত করতে পারে।
.prevent ঘটনা ঘটতে বাধা দেওয়া হবে। উদাহরণস্বরূপ এটি ডিফল্ট ফর্ম জমা ইভেন্ট ব্লক করতে ব্যবহার করা যেতে পারে। সব ঘটনা ঠেকানো যায় না।
.stop একটি বুদ্বুদ ইভেন্ট আরও ছড়িয়ে পড়া থেকে বন্ধ করা হয়। event.stopPropagation() বলা হয়।
.self ডিফল্টরূপে, ইভেন্টগুলি মূল উপাদানগুলিতে আপস্ট্রিমে প্রচারিত হয়, তাই একটি ইভেন্ট একাধিক ইভেন্ট শ্রোতাদের ট্রিগার করতে পারে। .self modifier ইভেন্ট শ্রোতাকে ট্রিগার করতে শুধুমাত্র তার নিজস্ব উপাদান থেকে ইভেন্টগুলিকে অনুমতি দেয়৷
.{keyAlias} ইভেন্ট হ্যান্ডলারকে শুধুমাত্র নির্দিষ্ট ইভেন্ট কীগুলিতে সাড়া দেওয়ার জন্য সীমাবদ্ধ করুন, উদাহরণস্বরূপ v-on:click.right বা v-on:keyup.s। হ্যান্ডলার অনুরোধ করতে পারে যে একই সাথে একাধিক কীস্ট্রোক প্রতিক্রিয়া জানাতে, যেমন: v-on:click.left.shift।
.left বাম বোতামে ক্লিক করুন।
.right ডান বোতামে ক্লিক করুন।
.middle মাঝের বোতামে ক্লিক করুন।

More Examples

Example 1

প্যারেন্ট এলিমেন্টে প্রথম ক্লিক ইভেন্ট ক্যাপচার করতে .capture মডিফায়ার ব্যবহার করে।

<template>
  <h1>v-on Example</h1>
  <p>When the '.capture' modifier is used on the parent DIV element, the event is captured first in the parent element when the child element is clicked.</p>
  <p>If the '.capture' modifier is removed from this code, the child element will capture the click event first. This is the default behavior, that the event bubbles up, first in child element, then to the parent.</p>
  <div v-on:click.capture="this.msg.push('parent')" id="parent">
    <p>This is the parent element.</p>
    <div v-on:click="this.msg.push('child')">
      <p>This is the child element. CLICK HERE!</p>
    </div>
  </div>
  <p>The order of when and where the event is captured.</p>
  <ol>
    <li v-for="x in msg">{{ x }}</li>
  </ol>
</template>

<script>
export default {
  data() {
    return {
      msg: []
    };
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
}
#parent {
  width: 250px;
  background-color: lightpink;
}
#parent > div {
  cursor: pointer;
  background-color: lightgreen;
}
</style>

Example 2

ইভেন্টের আরও বিস্তার রোধ করতে .stop মডিফায়ার ব্যবহার করে।

<template>
  <h1>v-on Example</h1>
  <p>The '.stop' modifier stops the click event from propagating any further.</p>
  <p>If the '.stop' modifier is removed from this code, the parent element will also capture the click event on the child element.</p>
  <div v-on:click="this.msg.push('parent')" id="parent">
    <p>This is the parent element.</p>
    <div v-on:click.stop="this.msg.push('child')">
      <p>This is the child element. CLICK HERE!</p>
    </div>
  </div>
  <p>The order of when and where the event is captured.</p>
  <ol>
    <li v-for="x in msg">{{ x }}</li>
  </ol>
</template>

<script>
export default {
  data() {
    return {
      msg: []
    };
  }
}
</script>

<style scoped>
div {
  margin: 10px;
  padding: 10px;
  border: dashed black 1px;
}
#parent {
  width: 250px;
  background-color: lightpink;
}
#parent > div {
  cursor: pointer;
  background-color: lightgreen;
}
</style>

Example 4

একাধিকবার ঘটতে না ঘটতে ইভেন্ট প্রতিরোধ করতে .one modifier ব্যবহার করে।

<template>
  <h1>v-on Example</h1>
  <p>The '.once' modifier prevents the event from happening more than once.</p>
  <button v-on:click.once="clickTimes++">Click</button>
  <p>Click event happened {{ clickTimes }} times.</p>
</template>

<script>
export default {
  data() {
    return {
      clickTimes: 0
    };
  }
}
</script>

Example 7

.left.shift মডিফায়ার ব্যবহার করে ইমেজ শিফট করার জন্য যখন ব্যবহারকারী শিফট কী ধরে বাম-ক্লিক করে।

<template>
  <h1>v-on Example</h1>
  <p>Hold 'Shift' key and press left mouse button on the image:</p>
  <img v-on:click.left.shift="changeImg" v-bind:src="imgUrl">
</template>

<script>
export default {
  data() {
    return {
      imgFish: true,
      imgUrl: 'img_fish.svg'
    }
  },
  methods: {
    changeImg() {
      this.imgFish = !this.imgFish;
      if (this.imgFish) {
        this.imgUrl = 'img_fish.svg'
      }
      else {
        this.imgUrl = 'img_tiger.svg'
      }
    }
  }
}
</script>

<style scoped>
img {
  width: 200px;
}
</style>

Exercise

প্রশিক্ষণ:

Vue.js v-on directive ?

:
✗ ভুল! এটি ভি-বাইন্ডের জন্য দাঁড়িয়েছে
@
✓ ঠিক আছে! ভি-অনের সংক্ষিপ্ত রূপ: @
#
✗ ভুল! # v-on একটি সংক্ষিপ্ত রূপ নয়
&
✗ ভুল! & v-on একটি সংক্ষিপ্ত রূপ নয়